icon: LiCodeSquare快速命令
快速命令工具让使用者能快速地搜索特定命令并执行。一些典型的命令如下:

用户在Network Editor,也就是节点编辑器面板,按下Ctrl+Space即可唤出快速命令搜索框,按照关键词搜索对应的命令,搜索后按下回车即可执行命令,按下Esc即可退出搜索窗口。
快速命令工具中的命令是可高度自定义的。用户目前可通过两种形式扩展命令。
快速命令工具提供了一个能完全自定义任何命令的框架。
$HOUDINI_USER_PREF_DIR/DolagPlugin/user_data/UserCustomConsoleItems.py文件。ConsoleItem(item_name, callback, alias=tuple(), important=False)USER_CUSTOM_ITEMS中,插件会自动读取加载此列表。# 1. 创建该指令的回调函数
def switch_selected_cb(context):
if len(context["selected_nodes"]) > 0:
network = context["network_node"]
switch_node = network.createNode("switch")
index = 0
for node in context["selected_nodes"]:
for i in range(len(node.outputNames())):
switch_node.setInput(index, node, i)
index += 1
switch_node.setPosition(context["editor_pos"])
# 2. 创建快捷指令项
tmp_item = ConsoleItem(item_name="Switch Selected", alias="switch", callback=switch_selected_cb)
# 3. 将创建的快捷指令添加到指令列表中
USER_CUSTOM_ITEMS.append(tmp_item)
回调函数会传入一个表示上下文的context参数,context参数记录了快速命令所需的一些属性,可作为字典访问对应属性。其键和值如下:
Houdini中的UI线程表现比较奇怪,在某些时候一些命令(比如subprocess.call, os.system等)会导致UI线程被阻塞,因此需要使用threading模块新开线程(进程)来避免阻塞。 例如快速命令中有Edit Code in VSC命令,打开VSC来编辑节点中的Vex和Python。其调用的函数使用了threading.thread来开启新线程(进程),以避免阻塞。调用的函数代码如下:
def openVexInVSC(vex_str):
if not isinstance(vex_str, str):
return
# escape quotes
vex_str = vex_str.replace("\"", "`")
# to avoid blocking houdini, we have to use threading
def __tmp_vex_vsc_func():
import subprocess
subprocess.call(["powershell", "-WindowStyle", "hidden", "-Command", r'''"{0}" | code -
exit'''.format(vex_str)], shell=False)
import threading
t = threading.Thread(target=__tmp_vex_vsc_func)
t.start()
用户可以在$HOUDINI_USER_PREF_DIR/DolagPlugin/user_data/user_console_items.json文件中添加自己的节点创建命令。
[
...
{
"item_name": "Subdivide",
"alias": "sd",
"important": true,
"type": "node"
"node_name": "subdivide"
}
]